package com.zh.common.picture

import android.content.Context
import android.graphics.Color
import com.luck.picture.lib.basic.PictureSelector
import com.luck.picture.lib.config.SelectMimeType
import com.luck.picture.lib.config.SelectModeConfig
import com.luck.picture.lib.entity.LocalMedia
import com.luck.picture.lib.interfaces.OnExternalPreviewEventListener
import com.luck.picture.lib.interfaces.OnResultCallbackListener
import com.yalantis.ucrop.UCrop

/**
 * @Author: wt
 * @Date: 2020/9/17 17:39
 * @Des:
 */
object PictureSelectorUtils {

    /**
     * 打开相册
     */
    fun openGallery(
        context: Context,
        selectionMedia: List<LocalMedia>?,
        maxSelectNum: Int = 1,
        cropEngine: ImageFileCropEngine? = null,
        callback: (Boolean, ArrayList<LocalMedia>) -> Unit?
    ) {
        PictureSelector.create(context)
            .openGallery(SelectMimeType.ofImage())
//            .setSelectorUIStyle(defPictureSelectorStyle())
            .setImageEngine(GlideEngine.createGlideEngine())//图片加载器
            .setCompressEngine(ImageFileCompressEngine())//压缩器
            .setCropEngine(cropEngine)//剪切器
            .isDisplayCamera(false)//是否显示相机入口
            .setSelectionMode(
                if (maxSelectNum <= 1) SelectModeConfig.SINGLE else SelectModeConfig.MULTIPLE
            )//单选或是多选
            .setMaxSelectNum(maxSelectNum)
            .setSelectedData(selectionMedia)
            .isPreviewImage(true)//是否支持预览图片
            .isPreviewFullScreenMode(true)//预览点击全屏效果
            .isEmptyResultReturn(false)//支持未选择返回
            .isSelectZoomAnim(true)//选择缩略图缩放效果
            .isOpenClickSound(false)//是否开启点击音效
            .isMaxSelectEnabledMask(true)//达到最大选择数是否开启禁选蒙层
            .isDirectReturnSingle(false)//单选时是否立即返回
            .forResult(object : OnResultCallbackListener<LocalMedia> {
                override fun onResult(result: ArrayList<LocalMedia>?) {
                    if (!result.isNullOrEmpty()) callback(false, result)
                }

                override fun onCancel() {

                }
            })
    }

    /**
     * 打开相机
     */
    fun openCamera(
        context: Context,
        cropEngine: ImageFileCropEngine? = null,
        callback: (Boolean, ArrayList<LocalMedia>) -> Unit?
    ) {
        PictureSelector.create(context)
            .openCamera(SelectMimeType.ofImage())
            .setCompressEngine(ImageFileCompressEngine())//压缩器
            .setCropEngine(cropEngine)
            .forResult(object : OnResultCallbackListener<LocalMedia> {
                override fun onResult(result: ArrayList<LocalMedia>?) {
                    if (!result.isNullOrEmpty()) callback(true, result)
                }

                override fun onCancel() {

                }
            })
    }

    /**
     * 圆形剪裁
     */
    fun circleCropEngine(isCircle: Boolean = true): ImageFileCropEngine {
        return ImageFileCropEngine(UCrop.Options().apply {
            val color = Color.parseColor("#7E73FF")
            setHideBottomControls(false)//隐藏底部工具
            // 是否让用户调整范围(默认false)，如果开启，可能会造成剪切的图片的长宽比不是设定的
            // 如果不开启，用户不能拖动选框，只能缩放图片
            setFreeStyleCropEnabled(false)
            //圆
            setCircleDimmedLayer(isCircle)
            withAspectRatio(1F, 1F)
            //不显示网格线
            setShowCropGrid(false)
            //不显示边框
            setShowCropFrame(false)
            setCircleStrokeColor(Color.WHITE)
            isCropDragSmoothToCenter(false)
            isForbidSkipMultipleCrop(true)
            setMaxScaleMultiplier(100F)
            setStatusBarColor(color)
            setToolbarColor(color)
            setToolbarWidgetColor(Color.WHITE)
        })
    }

    /**
     * 预览
     */
    fun openPreview(context: Context, position: Int, list: ArrayList<LocalMedia>) {
        PictureSelector.create(context)
            .openPreview()
            .setImageEngine(GlideEngine.createGlideEngine())
            .setExternalPreviewEventListener(object : OnExternalPreviewEventListener {
                override fun onPreviewDelete(position: Int) {

                }

                override fun onLongPressDownload(media: LocalMedia): Boolean {
                    return false
                }
            }).startActivityPreview(position, true, list)
    }

    /**
     * 相册风格
     */
    /*private fun defPictureSelectorStyle(): PictureSelectorStyle {
        val textColor = Color.parseColor("#131315")
        return PictureSelectorStyle().also { style ->
            style.selectMainStyle = SelectMainStyle().apply {
                isSelectNumberStyle = true//是否显示选择数量排号模式
//                isPreviewSelectNumberStyle = true//预览页勾选样式是否使用数量类型
                isPreviewDisplaySelectGallery = true//预览页是否显示选择画廊
//                previewSelectBackground = R.drawable.ps_checkbox_selector
//                previewSelectText = "选择"
//                previewSelectTextSize = 15
//                previewSelectTextColor = Color.WHITE
//                previewSelectMarginRight = 6.dp2px()
                selectBackground = com.luck.picture.lib.R.drawable.ps_default_num_selector
                selectNormalBackgroundResources =
                    com.luck.picture.lib.R.drawable.ps_select_complete_normal_bg
                selectNormalTextColor = com.qf.common.R.color.white_a50.color()
                selectNormalText = "完成"
                selectNormalTextSize = 14
                selectBackgroundResources = com.luck.picture.lib.R.drawable.ps_select_complete_bg
                selectText = R.string.ps_send_num.getString()
                selectTextColor = Color.WHITE
                selectTextSize = 14
                adapterPreviewGalleryBackgroundResource =
                    com.luck.picture.lib.R.drawable.ps_preview_gallery_bg
                adapterPreviewGalleryItemSize = 52.dp2px
                adapterItemSpacingSize = 2.dp2px
                isAdapterItemIncludeEdge = true
                mainListBackgroundColor = Color.parseColor("#F7F7F7")
            }
            style.titleBarStyle = TitleBarStyle().apply {
                titleBackgroundColor = Color.parseColor("#7E73FF")//标题栏背景
                previewTitleBackgroundColor = titleBackgroundColor//预览标题栏背景
                isHideCancelButton = true//是否隐藏取消按钮
            }

            style.bottomBarStyle = BottomNavBarStyle().apply {
                bottomNarBarBackgroundColor = Color.WHITE//底部NarBar背景色
                bottomNarBarHeight = 48.dp2px
                bottomPreviewNarBarBackgroundColor = R.color.white_a80.getColor()
                bottomSelectNumResources = R.drawable.bg_bottom_elect_num
                bottomPreviewNormalTextSize = 15
                bottomPreviewNormalTextColor = textColor
                bottomPreviewSelectTextColor = textColor
                isCompleteCountTips = false
            }
        }
    }*/
}